home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / gawk 2.11.1r3 / Sources / unix.d / gawk.1 < prev    next >
Encoding:
Text File  |  1991-06-01  |  32.8 KB  |  1,467 lines  |  [TEXT/MPS ]

  1. .TH GAWK 1 "August 24 1989" "Free Software Foundation"
  2. .SH NAME
  3. gawk \- pattern scanning and processing language
  4. .SH SYNOPSIS
  5. .B gawk
  6. .ig
  7. [
  8. .B \-d
  9. ] [
  10. .B \-D
  11. ]
  12. ..
  13. [
  14. .B \-a
  15. ] [
  16. .B \-e
  17. ] [
  18. .B \-c
  19. ] [
  20. .B \-C
  21. ] [
  22. .B \-V
  23. ] [
  24. .BI \-F\^ fs
  25. ] [
  26. .B \-v
  27. .IR var = val
  28. ]
  29. .B \-f
  30. .I program-file
  31. [
  32. .B \-\^\-
  33. ] file .\^.\^.
  34. .br
  35. .B gawk
  36. .ig
  37. [
  38. .B \-d
  39. ] [
  40. .B \-D
  41. ]
  42. ..
  43. [
  44. .B \-a
  45. ] [
  46. .B \-e
  47. ] [
  48. .B \-c
  49. ] [
  50. .B \-C
  51. ] [
  52. .B \-V
  53. ] [
  54. .BI \-F\^ fs
  55. ] [
  56. .B \-v
  57. .IR var = val
  58. ] [
  59. .B \-\^\-
  60. ]
  61. .I program-text
  62. file .\^.\^.
  63. .SH DESCRIPTION
  64. .I Gawk
  65. is the GNU Project's implementation of the AWK programming language.
  66. It conforms to the definition and description of the language in
  67. .IR "The AWK Programming Language" ,
  68. by Aho, Kernighan, and Weinberger,
  69. with the additional features defined in the System V Release 4 version
  70. of \s-1UNIX\s+1
  71. .IR awk ,
  72. and some GNU-specific extensions.
  73. .PP
  74. The command line consists of options to
  75. .I gawk
  76. itself, the AWK program text (if not supplied via the
  77. .B \-f
  78. option), and values to be made
  79. available in the
  80. .B ARGC
  81. and
  82. .B ARGV
  83. pre-defined AWK variables.
  84. .PP
  85. .I Gawk
  86. accepts the following options, which should be available on any implementation
  87. of the AWK language.
  88. .TP
  89. .BI \-F fs
  90. Use
  91. .I fs
  92. for the input field separator (the value of the
  93. .B FS
  94. predefined
  95. variable).
  96. .TP
  97. \fB\-v\fI var\fR\^=\^\fIval\fR
  98. Assign the value
  99. .IR val ,
  100. to the variable
  101. .IR var ,
  102. before execution of the program begins.
  103. Such variable values are available to the
  104. .B BEGIN
  105. block of an AWK program.
  106. .TP
  107. .BI \-f " program-file"
  108. Read the AWK program source from the file
  109. .IR program-file ,
  110. instead of from the first command line argument.
  111. Multiple
  112. .B \-f
  113. options may be used.
  114. .TP
  115. .B \-\^\-
  116. Signal the end of options. This is useful to allow further arguments to the
  117. AWK program itself to start with a ``\-''.
  118. This is mainly for consistency with the argument parsing convention used
  119. by most other System V programs.
  120. .PP
  121. The following options are specific to the GNU implementation.
  122. .TP
  123. .B \-a
  124. Use AWK style regular expressions as described in the book.
  125. This is the current default, but may not be when the POSIX P1003.2
  126. standard is finalized.
  127. It is orthogonal to
  128. .BR \-c .
  129. .TP
  130. .B \-e
  131. Use
  132. .IR egrep (1)
  133. style regular expressions as described in POSIX standard.
  134. This may become the default when the POSIX P1003.2
  135. standard is finalized.
  136. It is orthogonal to
  137. .BR \-c .
  138. .TP
  139. .B \-c
  140. Run in
  141. .I compatibility
  142. mode.  In compatibility mode,
  143. .I gawk
  144. behaves identically to \s-1UNIX\s+1
  145. .IR awk ;
  146. none of the GNU-specific extensions are recognized.
  147. .TP
  148. .B \-C
  149. Print the short version of the GNU copyright information message on
  150. the error output.
  151. This option may disappear in a future version of
  152. .IR gawk .
  153. .TP
  154. .B \-V
  155. Print version information for this particular copy of
  156. .I gawk
  157. on the error output.
  158. This is useful mainly for knowing if the current copy of
  159. .I gawk
  160. on your system
  161. is up to date with respect to whatever the Free Software Foundation
  162. is distributing.
  163. This option may disappear in a future version of
  164. .IR gawk .
  165. .PP
  166. Any other options are flagged as illegal, but are otherwise ignored.
  167. .PP
  168. An AWK program consists of a sequence of pattern-action statements
  169. and optional function definitions.
  170. .RS
  171. .PP
  172. \fIpattern\fB    { \fIaction statements\fB }\fR
  173. .br
  174. \fBfunction \fIname\fB(\fIparameter list\fB) { \fIstatements\fB }\fR
  175. .RE
  176. .PP
  177. .I Gawk
  178. first reads the program source from the
  179. .IR program-file (s)
  180. if specified, or from the first non-option argument on the command line.
  181. The
  182. .B \-f
  183. option may be used multiple times on the command line.
  184. .I Gawk
  185. will read the program text as if all the
  186. .IR program-file s
  187. had been concatenated together.  This is useful for building libraries
  188. of AWK functions, without having to include them in each new AWK
  189. program that uses them.  To use a library function in a file from a
  190. program typed in on the command line, specify
  191. .B /dev/tty
  192. as one of the
  193. .IR program-file s,
  194. type your program, and end it with a
  195. .B ^D
  196. (control-d).
  197. .PP
  198. The environment variable
  199. .B AWKPATH
  200. specifies a search path to use when finding source files named with
  201. the 
  202. .B \-f
  203. option.  If this variable does not exist, the default path is
  204. \fB".:/usr/lib/awk:/usr/local/lib/awk"\fR.
  205. If a file name given to the
  206. .B \-f
  207. option contains a ``/'' character, no path search is performed.
  208. .PP
  209. .I Gawk
  210. compiles the program into an internal form,
  211. executes the code in the
  212. .B BEGIN
  213. block(s) (if any),
  214. and then proceeds to read
  215. each file named in the
  216. .B ARGV
  217. array.
  218. If there are no files named on the command line,
  219. .I gawk
  220. reads the standard input.
  221. .PP
  222. If a ``file'' named on the command line has the form
  223. .IB var = val
  224. it is treated as a variable assignment. The variable
  225. .I var
  226. will be assigned the value
  227. .IR val .
  228. This is most useful for dynamically assigning values to the variables
  229. AWK uses to control how input is broken into fields and records. It
  230. is also useful for controlling state if multiple passes are needed over
  231. a single data file.
  232. .PP
  233. For each line in the input,
  234. .I gawk
  235. tests to see if it matches any
  236. .I pattern
  237. in the AWK program.
  238. For each pattern that the line matches, the associated
  239. .I action
  240. is executed.
  241. .SH VARIABLES AND FIELDS
  242. AWK variables are dynamic; they come into existence when they are
  243. first used. Their values are either floating-point numbers or strings,
  244. depending upon how they are used. AWK also has one dimension
  245. arrays; multiply dimensioned arrays may be simulated.
  246. There are several pre-defined variables that AWK sets as a program
  247. runs; these will be described as needed and summarized below.
  248. .SS Fields
  249. .PP
  250. As each input line is read,
  251. .I gawk
  252. splits the line into
  253. .IR fields ,
  254. using the value of the
  255. .B FS
  256. variable as the field separator.
  257. If
  258. .B FS
  259. is a single character, fields are separated by that character.
  260. Otherwise,
  261. .B FS
  262. is expected to be a full regular expression.
  263. In the special case that
  264. .B FS
  265. is a single blank, fields are separated
  266. by runs of blanks and/or tabs.
  267. Note that the value of
  268. .B IGNORECASE
  269. (see below) will also affect how fields are split when
  270. .B FS
  271. is a regular expression.
  272. .PP
  273. Each field in the input line may be referenced by its position,
  274. .BR $1 ,
  275. .BR $2 ,
  276. and so on.
  277. .B $0
  278. is the whole line. The value of a field may be assigned to as well.
  279. Fields need not be referenced by constants:
  280. .RS
  281. .PP
  282. .ft B
  283. n = 5
  284. .br
  285. print $n
  286. .ft R
  287. .RE
  288. .PP
  289. prints the fifth field in the input line.
  290. The variable
  291. .B NF
  292. is set to the total number of fields in the input line.
  293. .PP
  294. References to non-existent fields (i.e. fields after
  295. .BR $NF ),
  296. produce the null-string. However, assigning to a non-existent field
  297. (e.g., 
  298. .BR "$(NF+2) = 5" )
  299. will increase the value of
  300. .BR NF ,
  301. create any intervening fields with the null string as their value, and
  302. cause the value of
  303. .B $0
  304. to be recomputed, with the fields being separated by the value of
  305. .BR OFS .
  306. .SS Built-in Variables
  307. .PP
  308. AWK's built-in variables are:
  309. .PP
  310. .RS
  311. .TP \l'\fBIGNORECASE\fR'
  312. .B ARGC
  313. the number of command line arguments (does not include options to
  314. .IR gawk ,
  315. or the program source).
  316. .TP \l'\fBIGNORECASE\fR'
  317. .B ARGV
  318. array of command line arguments. The array is indexed from
  319. 0 to
  320. .B ARGC
  321. \- 1.
  322. Dynamically changing the contents of
  323. .B ARGV
  324. can control the files used for data.
  325. .TP \l'\fBIGNORECASE\fR'
  326. .B ENVIRON
  327. An array containing the values of the current environment.
  328. The array is indexed by the environment variables, each element being
  329. the value of that variable (e.g., \fBENVIRON["HOME"]\fP might be
  330. .BR /u/arnold ).
  331. Changing this array does not affect the environment seen by programs which
  332. .I gawk
  333. spawns via redirection or the
  334. .B system
  335. function.
  336. (This may change in a future version of
  337. .IR gawk .)
  338. .TP \l'\fBIGNORECASE\fR'
  339. .B FILENAME
  340. the name of the current input file.
  341. If no files are specified on the command line, the value of
  342. .B FILENAME
  343. is ``\-''.
  344. .TP \l'\fBIGNORECASE\fR'
  345. .B FNR
  346. the input record number in the current input file.
  347. .TP \l'\fBIGNORECASE\fR'
  348. .B FS
  349. the input field separator, a blank by default.
  350. .TP \l'\fBIGNORECASE\fR'
  351. .B IGNORECASE
  352. Controls the case-sensitivity of all regular expression operations. If
  353. .B IGNORECASE
  354. has a non-zero value, then pattern matching in rules,
  355. field splitting with
  356. .BR FS ,
  357. regular expression
  358. matching with
  359. .B ~
  360. and
  361. .BR !~ ,
  362. and the
  363. .BR gsub() ,
  364. .BR index() ,
  365. .BR match() ,
  366. .BR split() ,
  367. and
  368. .B sub()
  369. pre-defined functions will all ignore case when doing regular expression
  370. operations.  Thus, if
  371. .B IGNORECASE
  372. is not equal to zero,
  373. .B /aB/
  374. matches all of the strings \fB"ab"\fP, \fB"aB"\fP, \fB"Ab"\fP,
  375. and \fB"AB"\fP.
  376. As with all AWK variables, the initial value of
  377. .B IGNORECASE
  378. is zero, so all regular expression operations are normally case-sensitive.
  379. .TP \l'\fBIGNORECASE\fR'
  380. .B NF
  381. the number of fields in the current input record.
  382. .TP \l'\fBIGNORECASE\fR'
  383. .B NR
  384. the total number of input records seen so far.
  385. .TP \l'\fBIGNORECASE\fR'
  386. .B OFMT
  387. the output format for numbers,
  388. .B %.6g
  389. by default.
  390. .TP \l'\fBIGNORECASE\fR'
  391. .B OFS
  392. the output field separator, a blank by default.
  393. .TP \l'\fBIGNORECASE\fR'
  394. .B ORS
  395. the output record separator, by default a newline.
  396. .TP \l'\fBIGNORECASE\fR'
  397. .B RS
  398. the input record separator, by default a newline.
  399. .B RS
  400. is exceptional in that only the first character of its string
  401. value is used for separating records. If
  402. .B RS
  403. is set to the null string, then records are separated by
  404. blank lines.
  405. When
  406. .B RS
  407. is set to the null string, then the newline character always acts as
  408. a field separator, in addition to whatever value
  409. .B FS
  410. may have.
  411. .TP \l'\fBIGNORECASE\fR'
  412. .B RSTART
  413. the index of the first character matched by
  414. .BR match() ;
  415. 0 if no match.
  416. .TP \l'\fBIGNORECASE\fR'
  417. .B RLENGTH
  418. the length of the string matched by
  419. .BR match() ;
  420. \-1 if no match.
  421. .TP \l'\fBIGNORECASE\fR'
  422. .B SUBSEP
  423. the character used to separate multiple subscripts in array
  424. elements, by default \fB"\e034"\fR.
  425. .RE
  426. .SS Arrays
  427. .PP
  428. Arrays are subscripted with an expression between square brackets
  429. .RB ( [ " and " ] ).
  430. If the expression is an expression list
  431. .RI ( expr ", " expr " ...)"
  432. then the array subscript is a string consisting of the
  433. concatenation of the (string) value of each expression,
  434. separated by the value of the
  435. .B SUBSEP
  436. variable.
  437. This facility is used to simulate multiply dimensioned
  438. arrays. For example:
  439. .PP
  440. .RS
  441. .ft B
  442. i = "A" ;\^ j = "B" ;\^ k = "C"
  443. .br
  444. x[i, j, k] = "hello, world\en"
  445. .ft R
  446. .RE
  447. .PP
  448. assigns the string \fB"hello, world\en"\fR to the element of the array
  449. .B x
  450. which is indexed by the string \fB"A\e034B\e034C"\fR. All arrays in AWK
  451. are associative, i.e. indexed by string values.
  452. .PP
  453. The special operator
  454. .B in
  455. may be used in an
  456. .B if
  457. or
  458. .B while
  459. statement to see if an array has an index consisting of a particular
  460. value.
  461. .PP
  462. .RS
  463. .ft B
  464. .nf
  465. if (val in array)
  466.     print array[val]
  467. .fi
  468. .ft
  469. .RE
  470. .PP
  471. If the array has multiple subscripts, use
  472. .BR "(i, j) in array" .
  473. .PP
  474. The
  475. .B in
  476. construct may also be used in a
  477. .B for
  478. loop to iterate over all the elements of an array.
  479. .PP
  480. An element may be deleted from an array using the
  481. .B delete
  482. statement.
  483. .SS Variable Typing
  484. .PP
  485. Variables and fields
  486. may be (floating point) numbers, or strings, or both. How the
  487. value of a variable is interpreted depends upon its context. If used in
  488. a numeric expression, it will be treated as a number, if used as a string
  489. it will be treated as a string.
  490. .PP
  491. To force a variable to be treated as a number, add 0 to it; to force it
  492. to be treated as a string, concatenate it with the null string.
  493. .PP
  494. The AWK language defines comparisons as being done numerically if
  495. possible, otherwise one or both operands are converted to strings and
  496. a string comparison is performed.
  497. .PP
  498. Uninitialized variables have the numeric value 0 and the string value ""
  499. (the null, or empty, string).
  500. .SH PATTERNS AND ACTIONS
  501. AWK is a line oriented language. The pattern comes first, and then the
  502. action. Action statements are enclosed in
  503. .B {
  504. and
  505. .BR } .
  506. Either the pattern may be missing, or the action may be missing, but,
  507. of course, not both. If the pattern is missing, the action will be
  508. executed for every single line of input.
  509. A missing action is equivalent to
  510. .RS
  511. .PP
  512. .B "{ print }"
  513. .RE
  514. .PP
  515. which prints the entire line.
  516. .PP
  517. Comments begin with the ``#'' character, and continue until the
  518. end of the line.
  519. Blank lines may be used to separate statements.
  520. Normally, a statement ends with a newline, however, this is not the
  521. case for lines ending in
  522. a ``,'', ``{'', ``?'', ``:'', ``&&'', or ``||''.
  523. Lines ending in
  524. .B do
  525. or
  526. .B else
  527. also have their statements automatically continued on the following line.
  528. In other cases, a line can be continued by ending it with a ``\e'',
  529. in which case the newline will be ignored.
  530. .PP
  531. Multiple statements may
  532. be put on one line by separating them with a ``;''.
  533. This applies to both the statements within the action part of a
  534. pattern-action pair (the usual case),
  535. and to the pattern-action statements themselves.
  536. .SS Patterns
  537. AWK patterns may be one of the following:
  538. .PP
  539. .RS
  540. .nf
  541. .B BEGIN
  542. .B END
  543. .BI / "regular expression" /
  544. .I "relational expression"
  545. .IB pattern " && " pattern
  546. .IB pattern " || " pattern
  547. .IB pattern " ? " pattern " : " pattern
  548. .BI ( pattern )
  549. .BI ! " pattern"
  550. .IB pattern1 ", " pattern2"
  551. .fi
  552. .RE
  553. .PP
  554. .B BEGIN
  555. and
  556. .B END
  557. are two special kinds of patterns which are not tested against
  558. the input.
  559. The action parts of all
  560. .B BEGIN
  561. patterns are merged as if all the statements had
  562. been written in a single
  563. .B BEGIN
  564. block. They are executed before any
  565. of the input is read. Similarly, all the
  566. .B END
  567. blocks are merged,
  568. and executed when all the input is exhausted (or when an
  569. .B exit
  570. statement is executed).
  571. .B BEGIN
  572. and
  573. .B END
  574. patterns cannot be combined with other patterns in pattern expressions.
  575. .B BEGIN
  576. and
  577. .B END
  578. patterns cannot have missing action parts.
  579. .PP
  580. For
  581. .BI / "regular expression" /
  582. patterns, the associated statement is executed for each input line that matches
  583. the regular expression.
  584. Regular expressions are the same as those in
  585. .IR egrep (1),
  586. and are summarized below.
  587. .PP
  588. A
  589. .I "relational expression"
  590. may use any of the operators defined below in the section on actions.
  591. These generally test whether certain fields match certain regular expressions.
  592. .PP
  593. The
  594. .BR && ,
  595. .BR || ,
  596. and
  597. .B !
  598. operators are logical AND, logical OR, and logical NOT, respectively, as in C.
  599. They do short-circuit evaluation, also as in C, and are used for combining
  600. more primitive pattern expressions. As in most languages, parentheses
  601. may be used to change the order of evaluation.
  602. .PP
  603. The
  604. .B ?\^:
  605. operator is like the same operator in C. If the first pattern is true
  606. then the pattern used for testing is the second pattern, otherwise it is
  607. the third. Only one of the second and third patterns is evaluated.
  608. .PP
  609. The 
  610. .IB pattern1 ", " pattern2"
  611. form of an expression is called a range pattern.
  612. It matches all input lines starting with a line that matches
  613. .IR pattern1 ,
  614. and continuing until a line that matches
  615. .IR pattern2 ,
  616. inclusive. It does not combine with any other sort of pattern expression.
  617. .SS Regular Expressions
  618. Regular expressions are the extended kind found in
  619. .IR egrep .
  620. They are composed of characters as follows:
  621. .RS
  622. .TP \l'[^abc...]'
  623. .I c
  624. matches the non-metacharacter
  625. .IR c .
  626. .TP \l'[^abc...]'
  627. .I \ec
  628. matches the literal character
  629. .IR c .
  630. .TP \l'[^abc...]'
  631. .B .
  632. matches any character except newline.
  633. .TP \l'[^abc...]'
  634. .B ^
  635. matches the beginning of a line or a string.
  636. .TP \l'[^abc...]'
  637. .B $
  638. matches the end of a line or a string.
  639. .TP \l'[^abc...]'
  640. .BI [ abc... ]
  641. character class, matches any of the characters
  642. .IR abc... .
  643. .TP \l'[^abc...]'
  644. .BI [^ abc... ]
  645. negated character class, matches any character except
  646. .I abc...
  647. and newline.
  648. .TP \l'[^abc...]'
  649. .IB r1 | r2
  650. alternation: matches either
  651. .I r1
  652. or
  653. .IR r2 .
  654. .TP \l'[^abc...]'
  655. .I r1r2
  656. concatenation: matches
  657. .IR r1 ,
  658. and then
  659. .IR r2 .
  660. .TP \l'[^abc...]'
  661. .IB r +
  662. matches one or more
  663. .IR r 's. 
  664. .TP \l'[^abc...]'
  665. .IB r *
  666. matches zero or more
  667. .IR r 's. 
  668. .TP \l'[^abc...]'
  669. .IB r ?
  670. matches zero or one
  671. .IR r 's. 
  672. .TP \l'[^abc...]'
  673. .BI ( r )
  674. grouping: matches
  675. .IR r .
  676. .RE
  677. The escape sequences that are valid in string constants (see below)
  678. are also legal in regular expressions.
  679. .SS Actions
  680. Action statements are enclosed in braces,
  681. .B {
  682. and
  683. .BR } .
  684. Action statements consist of the usual assignment, conditional, and looping
  685. statements found in most languages. The operators, control statements,
  686. and input/output statements
  687. available are patterned after those in C.
  688. .SS Operators
  689. .PP
  690. The operators in AWK, in order of increasing precedence, are
  691. .PP
  692. .RS
  693. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  694. .B "= += \-= *= /= %= ^="
  695. Assignment. Both absolute assignment
  696. .BI ( var " = " value )
  697. and operator-assignment (the other forms) are supported.
  698. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  699. .B ?:
  700. The C conditional expression. This has the form
  701. .IB expr1 " ? " expr2 " : " expr3\c
  702. \&. If
  703. .I expr1
  704. is true, the value of the expression is
  705. .IR expr2 ,
  706. otherwise it is
  707. .IR expr3 .
  708. Only one of
  709. .I expr2
  710. and
  711. .I expr3
  712. is evaluated.
  713. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  714. .B ||
  715. logical OR.
  716. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  717. .B &&
  718. logical AND.
  719. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  720. .B "~ !~"
  721. regular expression match, negated match.
  722. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  723. .B "< <= > >= != =="
  724. the regular relational operators.
  725. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  726. .I blank
  727. string concatenation.
  728. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  729. .B "+ \-"
  730. addition and subtraction.
  731. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  732. .B "* / %"
  733. multiplication, division, and modulus.
  734. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  735. .B "+ \- !"
  736. unary plus, unary minus, and logical negation.
  737. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  738. .B ^
  739. exponentiation (\fB**\fR may also be used, and \fB**=\fR for
  740. the assignment operator).
  741. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  742. .B "++ \-\^\-"
  743. increment and decrement, both prefix and postfix.
  744. .TP \l'\fB= += \-= *= /= %= ^=\fR'
  745. .B $
  746. field reference.
  747. .RE
  748. .SS Control Statements
  749. .PP
  750. The control statements are
  751. as follows:
  752. .PP
  753. .RS
  754. .nf
  755. \fBif (\fIcondition\fB) \fIstatement\fR [ \fBelse\fI statement \fR]
  756. \fBwhile (\fIcondition\fB) \fIstatement \fR
  757. \fBdo \fIstatement \fBwhile (\fIcondition\fB)\fR
  758. \fBfor (\fIexpr1\fB; \fIexpr2\fB; \fIexpr3\fB) \fIstatement\fR
  759. \fBfor (\fIvar \fBin\fI array\fB) \fIstatement\fR
  760. \fBbreak\fR
  761. \fBcontinue\fR
  762. \fBdelete \fIarray\^\fB[\^\fIindex\^\fB]\fR
  763. \fBexit\fR [ \fIexpression\fR ]
  764. \fB{ \fIstatements \fB}
  765. .fi
  766. .RE
  767. .SS "I/O Statements"
  768. .PP
  769. The input/output statements are as follows:
  770. .PP
  771. .RS
  772. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  773. .BI close( filename )
  774. close file (or pipe, see below).
  775. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  776. .B getline
  777. set
  778. .B $0
  779. from next input record; set
  780. .BR NF ,
  781. .BR NR ,
  782. .BR FNR .
  783. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  784. .BI "getline <" file
  785. set
  786. .B $0
  787. from next record of
  788. .IR file ;
  789. set
  790. .BR NF .
  791. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  792. .BI getline " var"
  793. set
  794. .I var
  795. from next input record; set
  796. .BR NF ,
  797. .BR FNR .
  798. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  799. .BI getline " var" " <" file
  800. set
  801. .I var
  802. from next record of
  803. .IR file .
  804. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  805. .B next
  806. Stop processing the current input record. The next input record
  807. is read and processing starts over with the first pattern in the
  808. AWK program. If the end of the input data is reached, the
  809. .B END
  810. block(s), if any, are executed.
  811. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  812. .B print
  813. prints the current record.
  814. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  815. .BI print " expr-list"
  816. prints expressions.
  817. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  818. .BI print " expr-list" " >" file
  819. prints expressions on
  820. .IR file .
  821. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  822. .BI printf " fmt, expr-list"
  823. format and print.
  824. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  825. .BI printf " fmt, expr-list" " >" file
  826. format and print on
  827. .IR file .
  828. .TP \l'\fBprintf \fIfmt, expr-list\fR'
  829. .BI system( cmd-line )
  830. execute the command
  831. .IR cmd-line ,
  832. and return the exit status.
  833. (This may not be available on 
  834. systems besides \s-1UNIX\s+1 and \s-1GNU\s+1.)
  835. .RE
  836. .PP
  837. Other input/output redirections are also allowed. For
  838. .B print
  839. and
  840. .BR printf ,
  841. .BI >> file
  842. appends output to the
  843. .IR file ,
  844. while
  845. .BI | " command"
  846. writes on a pipe.
  847. In a similar fashion,
  848. .IB command " | getline"
  849. pipes into
  850. .BR getline .
  851. .BR Getline
  852. will return 0 on end of file, and \-1 on an error.
  853. .SS The \fIprintf\fP Statement
  854. .PP
  855. The AWK versions of the
  856. .B printf
  857. and
  858. .B sprintf
  859. (see below)
  860. functions accept the following conversion specification formats:
  861. .RS
  862. .TP
  863. .B %c
  864. An ASCII character.
  865. If the argument used for
  866. .B %c
  867. is numeric, it is treated as a character and printed.
  868. Otherwise, the argument is assumed to be a string, and the only first
  869. character of that string is printed.
  870. .TP
  871. .B %d
  872. A decimal number (the integer part).
  873. .TP
  874. .B %i
  875. Just like
  876. .BR %d .
  877. .TP
  878. .B %e
  879. A floating point number of the form
  880. .BR [\-]d.ddddddE[+\^\-]dd .
  881. .TP
  882. .B %f
  883. A floating point number of the form
  884. .BR [\-]ddd.dddddd .
  885. .TP
  886. .B %g
  887. Use
  888. .B e
  889. or
  890. .B f
  891. conversion, whichever is shorter, with nonsignificant zeros suppressed.
  892. .TP
  893. .B %o
  894. An unsigned octal number (again, an integer).
  895. .TP
  896. .B %s
  897. A character string.
  898. .TP
  899. .B %x
  900. An unsigned hexadecimal number (an integer).
  901. .TP
  902. .B %X
  903. Like
  904. .BR %x ,
  905. but using
  906. .B ABCDEF
  907. instead of
  908. .BR abcdef .
  909. .TP
  910. .B %%
  911. A single
  912. .B %
  913. character; no argument is converted.
  914. .RE
  915. .PP
  916. There are optional, additional parameters that may lie between the
  917. .B %
  918. and the control letter:
  919. .RS
  920. .TP
  921. .B \-
  922. The expression should be left-justified within its field.
  923. .TP
  924. .I width
  925. The field should be padded to this width. If the number has a leading
  926. zero, then the field will be padded with zeros.
  927. Otherwise it is padded with blanks.
  928. .TP
  929. .BI . prec
  930. A number indicating the maximum width of strings or digits to the right
  931. of the decimal point.
  932. .RE
  933. .PP
  934. The dynamic
  935. .I width
  936. and
  937. .I prec
  938. capabilities of the C library
  939. .B printf
  940. routines are not supported.
  941. However, they may be simulated by using
  942. the AWK concatenation operation to build up
  943. a format specification dynamically.
  944. .SS Special File Names
  945. .PP
  946. When doing I/O redirection from either
  947. .B print
  948. or
  949. .B printf
  950. into a file,
  951. or via
  952. .B getline
  953. from a file,
  954. .I gawk
  955. recognizes certain special filenames internally.  These filenames
  956. allow access to open file descriptors inherited from
  957. .IR gawk 's
  958. parent process (usually the shell).  The filenames are:
  959. .RS
  960. .TP
  961. .B /dev/stdin
  962. The standard input.
  963. .TP
  964. .B /dev/stdout
  965. The standard output.
  966. .TP
  967. .B /dev/stderr
  968. The standard error output.
  969. .TP
  970. .BI /dev/fd/\^ n
  971. The file denoted by the open file descriptor
  972. .IR n .
  973. .RE
  974. .PP
  975. These are particularly useful for error messages. For example:
  976. .PP
  977. .RS
  978. .ft B
  979. print "You blew it!" > "/dev/stderr"
  980. .ft R
  981. .RE
  982. .PP
  983. whereas you would otherwise have to use
  984. .PP
  985. .RS
  986. .ft B
  987. print "You blew it!" | "cat 1>&2"
  988. .ft R
  989. .RE
  990. .PP
  991. These file names may also be used on the command line to name data files.
  992. .SS Numeric Functions
  993. .PP
  994. AWK has the following pre-defined arithmetic functions:
  995. .PP
  996. .RS
  997. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  998. .BI atan2( y , " x" )
  999. returns the arctangent of
  1000. .I y/x
  1001. in radians.
  1002. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  1003. .BI cos( expr )
  1004. returns the cosine in radians.
  1005. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  1006. .BI exp( expr )
  1007. the exponential function.
  1008. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  1009. .BI int( expr )
  1010. truncates to integer.
  1011. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  1012. .BI log( expr )
  1013. the natural logarithm function.
  1014. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  1015. .B rand()
  1016. returns a random number between 0 and 1.
  1017. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  1018. .BI sin( expr )
  1019. returns the sine in radians.
  1020. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  1021. .BI sqrt( expr )
  1022. the square root function.
  1023. .TP \l'\fBsrand(\fIexpr\fB)\fR'
  1024. .BI srand( expr )
  1025. use
  1026. .I expr
  1027. as a new seed for the random number generator. If no
  1028. .I expr
  1029. is provided, the time of day will be used.
  1030. The return value is the previous seed for the random
  1031. number generator.
  1032. .RE
  1033. .SS String Functions
  1034. .PP
  1035. AWK has the following pre-defined string functions:
  1036. .PP
  1037. .RS
  1038. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1039. \fBgsub(\fIr\fB, \fIs\fB, \fIt\fB)\fR
  1040. for each substring matching the regular expression
  1041. .I r
  1042. in the string
  1043. .IR t ,
  1044. substitute the string
  1045. .IR s ,
  1046. and return the number of substitutions.
  1047. If
  1048. .I t
  1049. is not supplied, use
  1050. .BR $0 .
  1051. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1052. .BI index( s , " t" )
  1053. returns the index of the string
  1054. .I t
  1055. in the string
  1056. .IR s ,
  1057. or 0 if
  1058. .I t
  1059. is not present.
  1060. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1061. .BI length( s )
  1062. returns the length of the string
  1063. .IR s .
  1064. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1065. .BI match( s , " r" )
  1066. returns the position in
  1067. .I s
  1068. where the regular expression
  1069. .I r
  1070. occurs, or 0 if
  1071. .I r
  1072. is not present, and sets the values of
  1073. .B RSTART
  1074. and
  1075. .BR RLENGTH .
  1076. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1077. \fBsplit(\fIs\fB, \fIa\fB, \fIr\fB)\fR
  1078. splits the string
  1079. .I s
  1080. into the array
  1081. .I a
  1082. on the regular expression
  1083. .IR r ,
  1084. and returns the number of fields. If
  1085. .I r
  1086. is omitted,
  1087. .B FS
  1088. is used instead.
  1089. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1090. .BI sprintf( fmt , " expr-list" )
  1091. prints
  1092. .I expr-list
  1093. according to
  1094. .IR fmt ,
  1095. and returns the resulting string.
  1096. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1097. \fBsub(\fIr\fB, \fIs\fB, \fIt\fB)\fR
  1098. this is just like
  1099. .BR gsub ,
  1100. but only the first matching substring is replaced.
  1101. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1102. \fBsubstr(\fIs\fB, \fIi\fB, \fIn\fB)\fR
  1103. returns the
  1104. .IR n -character
  1105. substring of
  1106. .I s
  1107. starting at
  1108. .IR i .
  1109. If
  1110. .I n
  1111. is omitted, the rest of
  1112. .I s
  1113. is used.
  1114. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1115. .BI tolower( str )
  1116. returns a copy of the string
  1117. .IR str ,
  1118. with all the upper-case characters in
  1119. .I str
  1120. translated to their corresponding lower-case counterparts.
  1121. Non-alphabetic characters are left unchanged.
  1122. .TP \l'\fBsprintf(\fIfmt\fB, \fIexpr-list\fB)\fR'
  1123. .BI toupper( str )
  1124. returns a copy of the string
  1125. .IR str ,
  1126. with all the lower-case characters in
  1127. .I str
  1128. translated to their corresponding upper-case counterparts.
  1129. Non-alphabetic characters are left unchanged.
  1130. .RE
  1131. .SS String Constants
  1132. .PP
  1133. String constants in AWK are sequences of characters enclosed
  1134. between double quotes (\fB"\fR). Within strings, certain
  1135. .I "escape sequences"
  1136. are recognized, as in C. These are:
  1137. .PP
  1138. .RS
  1139. .TP \l'\fB\e\fIddd\fR'
  1140. .B \e\e
  1141. A literal backslash.
  1142. .TP \l'\fB\e\fIddd\fR'
  1143. .B \ea
  1144. The ``alert'' character; usually the ASCII BEL character.
  1145. .TP \l'\fB\e\fIddd\fR'
  1146. .B \eb
  1147. backspace.
  1148. .TP \l'\fB\e\fIddd\fR'
  1149. .B \ef
  1150. form-feed.
  1151. .TP \l'\fB\e\fIddd\fR'
  1152. .B \en
  1153. new line.
  1154. .TP \l'\fB\e\fIddd\fR'
  1155. .B \er
  1156. carriage return.
  1157. .TP \l'\fB\e\fIddd\fR'
  1158. .B \et
  1159. horizontal tab.
  1160. .TP \l'\fB\e\fIddd\fR'
  1161. .B \ev
  1162. vertical tab.
  1163. .TP \l'\fB\e\fIddd\fR'
  1164. .BI \ex "\^hex digits"
  1165. The character represented by the string of hexadecimal digits following
  1166. the
  1167. .BR \ex .
  1168. As in ANSI C, all following hexadecimal digits are considered part of
  1169. the escape sequence.
  1170. (This feature should tell us something about language design by committee.)
  1171. E.g., "\ex1B" is the ASCII ESC (escape) character.
  1172. .TP \l'\fB\e\fIddd\fR'
  1173. .BI \e ddd
  1174. The character represented by the 1-, 2-, or 3-digit sequence of octal
  1175. digits. E.g. "\e033" is the ASCII ESC (escape) character.
  1176. .TP \l'\fB\e\fIddd\fR'
  1177. .BI \e c
  1178. The literal character
  1179. .IR c\^ .
  1180. .RE
  1181. .PP
  1182. The escape sequences may also be used inside constant regular expressions
  1183. (e.g.,
  1184. .B "/[\ \et\ef\en\er\ev]/"
  1185. matches whitespace characters).
  1186. .SH FUNCTIONS
  1187. Functions in AWK are defined as follows:
  1188. .PP
  1189. .RS
  1190. \fBfunction \fIname\fB(\fIparameter list\fB) { \fIstatements \fB}\fR
  1191. .RE
  1192. .PP
  1193. Functions are executed when called from within the action parts of regular
  1194. pattern-action statements. Actual parameters supplied in the function
  1195. call are used to instantiate the formal parameters declared in the function.
  1196. Arrays are passed by reference, other variables are passed by value.
  1197. .PP
  1198. Since functions were not originally part of the AWK language, the provision
  1199. for local variables is rather clumsy: they are declared as extra parameters
  1200. in the parameter list. The convention is to separate local variables from
  1201. real parameters by extra spaces in the parameter list. For example:
  1202. .PP
  1203. .RS
  1204. .ft B
  1205. .nf
  1206. function  f(p, q,     a, b) {    # a & b are local
  1207.             ..... }
  1208.  
  1209. /abc/    { ... ; f(1, 2) ; ... }
  1210. .fi
  1211. .ft R
  1212. .RE
  1213. .PP
  1214. The left parenthesis in a function call is required
  1215. to immediately follow the function name,
  1216. without any intervening white space.
  1217. This is to avoid a syntactic ambiguity with the concatenation operator.
  1218. This restriction does not apply to the built-in functions listed above.
  1219. .PP
  1220. Functions may call each other and may be recursive.
  1221. Function parameters used as local variables are initialized
  1222. to the null string and the number zero upon function invocation.
  1223. .PP
  1224. The word
  1225. .B func
  1226. may be used in place of
  1227. .BR function .
  1228. .SH EXAMPLES
  1229. .nf
  1230. Print and sort the login names of all users:
  1231.  
  1232. .ft B
  1233.     BEGIN    { FS = ":" }
  1234.         { print $1 | "sort" }
  1235.  
  1236. .ft R
  1237. Count lines in a file:
  1238.  
  1239. .ft B
  1240.         { nlines++ }
  1241.     END    { print nlines }
  1242.  
  1243. .ft R
  1244. Precede each line by its number in the file:
  1245.  
  1246. .ft B
  1247.     { print FNR, $0 }
  1248.  
  1249. .ft R
  1250. Concatenate and line number (a variation on a theme):
  1251.  
  1252. .ft B
  1253.     { print NR, $0 }
  1254. .ft R
  1255. .fi
  1256. .SH SEE ALSO
  1257. .IR egrep (1)
  1258. .PP
  1259. .IR "The AWK Programming Language" ,
  1260. Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger,
  1261. Addison-Wesley, 1988. ISBN 0-201-07981-X.
  1262. .PP
  1263. .IR "The GAWK Manual" ,
  1264. published by the Free Software Foundation, 1989.
  1265. .SH SYSTEM V RELEASE 4 COMPATIBILITY
  1266. A primary goal for
  1267. .I gawk
  1268. is compatibility with the latest version of \s-1UNIX\s+1
  1269. .IR awk .
  1270. To this end,
  1271. .I gawk
  1272. incorporates the following user visible
  1273. features which are not described in the AWK book,
  1274. but are part of
  1275. .I awk
  1276. in System V Release 4.
  1277. .PP
  1278. The
  1279. .B \-v
  1280. option for assigning variables before program execution starts is new.
  1281. The book indicates that command line variable assignment happens when
  1282. .I awk
  1283. would otherwise open the argument as a file, which is after the
  1284. .B BEGIN
  1285. block is executed.  However, in earlier implementations, when such an
  1286. assignment appeared before any file names, the assignment would happen
  1287. .I before
  1288. the
  1289. .B BEGIN
  1290. block was run.  Applications came to depend on this ``feature.''
  1291. When
  1292. .I awk
  1293. was changed to match its documentation, this option was added to
  1294. accomodate applications that depended upon the old behaviour.
  1295. .PP
  1296. When processing arguments,
  1297. .I gawk
  1298. uses the special option ``\fB\-\^\-\fP'' to signal the end of
  1299. arguments, and warns about, but otherwise ignores, undefined options.
  1300. .PP
  1301. The AWK book does not define the return value of
  1302. .BR srand() .
  1303. The System V Release 4 version of \s-1UNIX\s+1
  1304. .I awk
  1305. has it return the seed it was using, to allow keeping track
  1306. of random number sequences. Therefore
  1307. .B srand()
  1308. in
  1309. .I gawk
  1310. also returns its current seed.
  1311. .PP
  1312. Other new features are:
  1313. The use of multiple
  1314. .B \-f
  1315. options; the
  1316. .B ENVIRON
  1317. array; the
  1318. .BR \ea ,
  1319. and
  1320. .BR \ev ,
  1321. .B \ex
  1322. escape sequences; the
  1323. .B tolower
  1324. and
  1325. .B toupper
  1326. built-in functions; and the ANSI C conversion specifications in
  1327. .BR printf .
  1328. .SH GNU EXTENSIONS
  1329. .I Gawk
  1330. has some extensions to System V
  1331. .IR awk .
  1332. They are described in this section.  All the extensions described here
  1333. can be disabled by compiling
  1334. .I gawk
  1335. with
  1336. .BR \-DSTRICT ,
  1337. or by invoking
  1338. .I gawk
  1339. with the
  1340. .B \-c
  1341. option.
  1342. If the underlying operating system supports the
  1343. .B /dev/fd
  1344. directory and corresponding files, then
  1345. .I gawk
  1346. can be compiled with
  1347. .B \-DNO_DEV_FD
  1348. to disable the special filename processing.
  1349. .PP
  1350. The following features of
  1351. .I gawk
  1352. are not available in
  1353. System V
  1354. .IR awk .
  1355. .RS
  1356. .TP \l'\(bu'
  1357. \(bu
  1358. The special file names available for I/O redirection are not recognized.
  1359. .TP \l'\(bu'
  1360. \(bu
  1361. The
  1362. .B IGNORECASE
  1363. variable and its side-effects are not available.
  1364. .TP \l'\(bu'
  1365. \(bu
  1366. No path search is performed for files named via the
  1367. .B \-f
  1368. option.  Therefore the
  1369. .B AWKPATH
  1370. environment variable is not special.
  1371. .TP \l'\(bu'
  1372. \(bu
  1373. The
  1374. .BR \-a ,
  1375. .BR \-e ,
  1376. .BR \-c ,
  1377. .BR \-C ,
  1378. and
  1379. .B \-V
  1380. command line options.
  1381. .RE
  1382. .PP
  1383. The AWK book does not define the return value of the
  1384. .B close
  1385. function.
  1386. .IR Gawk\^ 's
  1387. .B close
  1388. returns the value from
  1389. .IR fclose (3),
  1390. or
  1391. .IR pclose (3),
  1392. when closing a file or pipe, respectively.
  1393. .PP
  1394. When
  1395. .I gawk
  1396. is invoked with the
  1397. .B \-c
  1398. option,
  1399. if the
  1400. .I fs
  1401. argument to the
  1402. .B \-F
  1403. option is ``t'', then
  1404. .B FS
  1405. will be set to the tab character.
  1406. Since this is a rather ugly special case, it is not the default behavior.
  1407. .ig
  1408. .PP
  1409. The rest of the features described in this section may change at some time in
  1410. the future, or may go away entirely.
  1411. You should not write programs that depend upon them.
  1412. .PP
  1413. .I Gawk
  1414. accepts the following additional options:
  1415. .TP
  1416. .B \-D
  1417. Turn on general debugging and turn on
  1418. .IR yacc (1)
  1419. or
  1420. .IR bison (1)
  1421. debugging output during program parsing.
  1422. This option should only be of interest to the
  1423. .I gawk
  1424. maintainers, and may not even be compiled into
  1425. .IR gawk .
  1426. .TP
  1427. .B \-d
  1428. Turn on general debugging and print the
  1429. .I gawk
  1430. internal tree as the program is executed.
  1431. This option should only be of interest to the
  1432. .I gawk
  1433. maintainers, and may not even be compiled into
  1434. .IR gawk .
  1435. ..
  1436. .SH BUGS
  1437. The
  1438. .B \-F
  1439. option is not necessary given the command line variable assignment feature;
  1440. it remains only for backwards compatibility.
  1441. .PP
  1442. There are now too many options.
  1443. Fortunately, most of them are rarely needed.
  1444. .SH AUTHORS
  1445. The original version of \s-1UNIX\s+1
  1446. .I awk
  1447. was designed and implemented by Alfred Aho,
  1448. Peter Weinberger, and Brian Kernighan of AT&T Bell Labs. Brian Kernighan
  1449. continues to maintain and enhance it.
  1450. .PP
  1451. Paul Rubin and Jay Fenlason,
  1452. of the Free Software Foundation, wrote
  1453. .IR gawk ,
  1454. to be compatible with the original version of
  1455. .I awk
  1456. distributed in Seventh Edition \s-1UNIX\s+1.
  1457. John Woods contributed a number of bug fixes.
  1458. David Trueman of Dalhousie University, with contributions
  1459. from Arnold Robbins at Emory University, made
  1460. .I gawk
  1461. compatible with the new version of \s-1UNIX\s+1
  1462. .IR awk .
  1463. .SH ACKNOWLEDGEMENTS
  1464. Brian Kernighan of Bell Labs
  1465. provided valuable assistance during testing and debugging.
  1466. We thank him.
  1467.